package com.hlt.shuiliting.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlt.shuiliting.dao.BiaoDuanDAO;
import com.hlt.shuiliting.dao.GongChengXMDAO;
import com.hlt.shuiliting.valuebean.BiaoDuan;

public class BiaoDuanServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public BiaoDuanServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");//�������Ĳ���
		response.setCharacterEncoding("UTF-8");//�������Ĳ���
		response.setContentType("text/html");
		
		String servletPath = request.getServletPath();
		if (servletPath.equalsIgnoreCase("/listBiaoDuan")) {
			 listBiaoDuan(request, response);
		} else if (servletPath.equalsIgnoreCase("/viewBiaoDuan")) {
			viewBiaoDuan(request, response);
		} else if (servletPath.equalsIgnoreCase("/insertBiaoDuan")) {
			insertBiaoDuan(request, response);
		} else if (servletPath.equalsIgnoreCase("/updateBiaoDuan")) {
			updateBiaoDuan(request, response);
		} else if (servletPath.equalsIgnoreCase("/deleteBiaoDuan")) {
			deleteBiaoDuan(request, response);
		}
	}
	
	public void listBiaoDuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BiaoDuanDAO biaoDuanDAO = new BiaoDuanDAO();
		GongChengXMDAO gongChengXMDAO = new GongChengXMDAO();
		
		String pageSize = request.getParameter("pageSize");
		String pageNum = request.getParameter("pageNum");
		
		String GongChengXMID = request.getParameter("GongChengXMID");
		List list = biaoDuanDAO.list(Integer.parseInt(GongChengXMID), (Integer.parseInt(pageSize)), (Integer.parseInt(pageNum)));
		String pageCount = Integer.toString(biaoDuanDAO.getPageCount());
		Map map = (Map)gongChengXMDAO.view(Integer.parseInt(GongChengXMID));
		
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("gongChengXM", map);
		request.setAttribute("biaoDuanLIST", list);
		
		request.getRequestDispatcher("/BiaoDuan_List.jsp").forward(request, response);
	}
	
	public void viewBiaoDuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BiaoDuanDAO biaoDuanDAO = new BiaoDuanDAO();
		String ID = request.getParameter("ID");
		String viewOrUpdate = request.getParameter("viewOrUpdate");
		Map map = biaoDuanDAO.view(Integer.parseInt(ID));
		request.setAttribute("biaoDuan", map);
		
		if (viewOrUpdate.equalsIgnoreCase("view")) {
			request.getRequestDispatcher("BiaoDuan_View.jsp").forward(request, response);
		} else if (viewOrUpdate.equalsIgnoreCase("update")) {
			request.getRequestDispatcher("BiaoDuan_Update.jsp").forward(request, response);
		}
	}
	
	public void insertBiaoDuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BiaoDuanDAO biaoDuanDAO = new BiaoDuanDAO();
		BiaoDuan biaoDuan = new BiaoDuan();
		
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String BiaoDuanMC = request.getParameter("BiaoDuanMC");
		String FaBaoFS = request.getParameter("FaBaoFS");
		String ZhaoBiaoZZXS = request.getParameter("ZhaoBiaoZZXS");
		String ZhaoBiaoSX = request.getParameter("ZhaoBiaoSX");
		String ZhaoBiaoJDBM = request.getParameter("ZhaoBiaoJDBM");
		String ZhaoBiaoDLJG = request.getParameter("ZhaoBiaoDLJG");
		String ZhongBiaoDW = request.getParameter("ZhongBiaoDW");
		String ZhongBiaoJG = request.getParameter("ZhongBiaoJG");
		String HeTongJHZGQ = request.getParameter("HeTongJHZGQ");
		String JiHuaKGRQ = request.getParameter("JiHuaKGRQ");
		String JiHuaWGRQ = request.getParameter("JiHuaWGRQ");
		String QiYeID = request.getParameter("QiYeID");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		biaoDuan.setZhuangTai(ZhuangTai);
		biaoDuan.setJiLuSJ(JiLuSJ);	
		biaoDuan.setLuRuR(LuRuR);	
		biaoDuan.setBiaoDuanMC(BiaoDuanMC);	
		biaoDuan.setFaBaoFS(FaBaoFS);	
		biaoDuan.setZhaoBiaoZZXS(ZhaoBiaoZZXS);	
		biaoDuan.setZhaoBiaoSX(ZhaoBiaoSX);	
		biaoDuan.setZhaoBiaoJDBM(ZhaoBiaoJDBM);	
		biaoDuan.setZhaoBiaoDLJG(ZhaoBiaoDLJG);	
		biaoDuan.setZhongBiaoDW(ZhongBiaoDW);	
		biaoDuan.setZhongBiaoJG(ZhongBiaoJG);	
		biaoDuan.setHeTongJHZGQ(HeTongJHZGQ);	
		biaoDuan.setJiHuaKGRQ(JiHuaKGRQ);	
		biaoDuan.setJiHuaWGRQ(JiHuaWGRQ);	
		biaoDuan.setQiYeID(QiYeID);	
		biaoDuan.setGongChengXMID(GongChengXMID);
		
		biaoDuanDAO.insert(biaoDuan);
		
		response.sendRedirect("listBiaoDuan?GongChengXMID=" + GongChengXMID);
	}

	public void updateBiaoDuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BiaoDuanDAO biaoDuanDAO = new BiaoDuanDAO();
		BiaoDuan biaoDuan = new BiaoDuan();
		
		String ID = request.getParameter("ID");
		String ZhuangTai = request.getParameter("ZhuangTai");
		String JiLuSJ = request.getParameter("JiLuSJ");
		String LuRuR = request.getParameter("LuRuR");
		String BiaoDuanMC = request.getParameter("BiaoDuanMC");
		String FaBaoFS = request.getParameter("FaBaoFS");
		String ZhaoBiaoZZXS = request.getParameter("ZhaoBiaoZZXS");
		String ZhaoBiaoSX = request.getParameter("ZhaoBiaoSX");
		String ZhaoBiaoJDBM = request.getParameter("ZhaoBiaoJDBM");
		String ZhaoBiaoDLJG = request.getParameter("ZhaoBiaoDLJG");
		String ZhongBiaoDW = request.getParameter("ZhongBiaoDW");
		String ZhongBiaoJG = request.getParameter("ZhongBiaoJG");
		String HeTongJHZGQ = request.getParameter("HeTongJHZGQ");
		String JiHuaKGRQ = request.getParameter("JiHuaKGRQ");
		String JiHuaWGRQ = request.getParameter("JiHuaWGRQ");
		String QiYeID = request.getParameter("QiYeID");
		String GongChengXMID = request.getParameter("GongChengXMID");
		
		biaoDuan.setID(ID);
		biaoDuan.setZhuangTai(ZhuangTai);
		biaoDuan.setJiLuSJ(JiLuSJ);	
		biaoDuan.setLuRuR(LuRuR);	
		biaoDuan.setBiaoDuanMC(BiaoDuanMC);	
		biaoDuan.setFaBaoFS(FaBaoFS);	
		biaoDuan.setZhaoBiaoZZXS(ZhaoBiaoZZXS);	
		biaoDuan.setZhaoBiaoSX(ZhaoBiaoSX);	
		biaoDuan.setZhaoBiaoJDBM(ZhaoBiaoJDBM);	
		biaoDuan.setZhaoBiaoDLJG(ZhaoBiaoDLJG);	
		biaoDuan.setZhongBiaoDW(ZhongBiaoDW);	
		biaoDuan.setZhongBiaoJG(ZhongBiaoJG);	
		biaoDuan.setHeTongJHZGQ(HeTongJHZGQ);	
		biaoDuan.setJiHuaKGRQ(JiHuaKGRQ);	
		biaoDuan.setJiHuaWGRQ(JiHuaWGRQ);	
		biaoDuan.setQiYeID(QiYeID);	
		biaoDuan.setGongChengXMID(GongChengXMID);
		
		biaoDuanDAO.update(biaoDuan);
		
		response.sendRedirect("listBiaoDuan?GongChengXMID=" + GongChengXMID);
	}
	
	public void deleteBiaoDuan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BiaoDuanDAO biaoDuanDAO = new BiaoDuanDAO();
		String ID = request.getParameter("ID");
		String GongChengXMID = request.getParameter("gongChengXMID");
		biaoDuanDAO.delete(Integer.parseInt(ID));
		response.sendRedirect("listBiaoDuan?GongChengXMID=" + GongChengXMID);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
